package com.grp6.game.chess;

import java.util.ArrayList;

import com.grp6.game.chess.model.ChessGameBoard;
import com.grp6.game.chess.model.ChessGameModel;
import com.grp6.game.chess.model.ChessPiece;
import com.grp6.game.chess.model.ChessPiece.Color;
import com.grp6.game.chess.model.ChessPiece.Figure;
import com.grp6.game.chess.model.ChessPlace;
import com.grp6.game.chess.player.ChessComputerPlayer;
import com.grp6.game.chess.player.ChessHumanPlayer;
import com.grp6.game.chess.player.IChessPlayer;
import com.grp6.game.chess.view.ChessGameView;
import com.grp6.game.chess.view.ChessPieceView;
import com.grp6.game.chess.view.ChessPlaceView;
import com.grp6.game.controller.AbstractGameLifecycle;
import com.grp6.game.controller.IGameBuilder;
import com.grp6.game.ui.platform.IAbstractUIPlatformFactory;
import com.grp6.game.ui.platform.ICell;

public class ChessGameBuilder implements IGameBuilder {
	private static final Figure[][] pFigures = new Figure[][]{
		{Figure.Rook,Figure.Knight,Figure.Bishop,Figure.King,Figure.Queen,Figure.Bishop,Figure.Knight,Figure.Rook},
		{Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn,Figure.Pawn},
		{Figure.Rook,Figure.Knight,Figure.Bishop,Figure.King,Figure.Queen,Figure.Bishop,Figure.Knight,Figure.Rook}
	};
	private static final Color[][] pColors = new Color[][]{
		{Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black},
		{Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black,Color.Black},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{null,null,null,null,null,null,null,null},
		{Color.White,Color.White,Color.White,Color.White,Color.White,Color.White,Color.White,Color.White},
		{Color.White,Color.White,Color.White,Color.White,Color.White,Color.White,Color.White,Color.White}
	};
	
	
	private final IAbstractUIPlatformFactory ui_fac;
	private final ChessGameFactory game_fac;

	public ChessGameBuilder(IAbstractUIPlatformFactory uifac, ChessGameFactory chessGameFactory) {
		this.ui_fac = uifac;
		this.game_fac = chessGameFactory;
	}

	@Override
	public AbstractGameLifecycle build() {
		ChessPreferences prefs = game_fac.createPreferences(ui_fac);
		
		int numOfPlayers = prefs.getNumOfPlayers();
		ArrayList<IChessPlayer> players = new ArrayList<IChessPlayer>();
		for(int i=0;i<numOfPlayers;i++){
			Class<IChessPlayer> playerType = (Class<IChessPlayer>) prefs.getPlayerType(i);
			if (playerType.isAssignableFrom(ChessComputerPlayer.class))
				players.add(game_fac.createComputerPlayer());
			else if (playerType.isAssignableFrom(ChessHumanPlayer.class))
				players.add(game_fac.createHumanPlayer());
		}
		
		ChessGameBoard board = game_fac.createGameBoard(prefs);
		ChessGameModel gameModel = game_fac.createGameModel(board,players);
		
		ChessGameView gameView = new ChessGameView();
		gameView.setModel(gameModel);
		
		
		
		for(int x=0;x<8;x++)
			for(int y=0;y<8;y++){
				ICell cell = ui_fac.createICell(x, y);
				
				boolean black = (x+y)%2==1;
				ChessPlace place = new ChessPlace(black);
				ChessPlaceView placeView = new ChessPlaceView();
				placeView.setModel(place);
				placeView.addCell(cell);
				gameView.getSubViews().add(placeView);
				board.getPlaces().add(place);
				
				if (pFigures[x][y]!=null && pColors[x][y]!=null){
					ChessPiece piece = new ChessPiece(pFigures[x][y],pColors[x][y]);
					ChessPieceView pieceView = new ChessPieceView();
					pieceView.setModel(piece);
					pieceView.addCell(cell);
					gameView.getSubViews().add(pieceView);
					board.getPieces().add(piece);
					
					IChessPlayer player = players.get(pColors[x][y].ordinal());
					player.getPieces().add(piece);
					place.getPieces().add(piece);
					piece.setPlace(place);
				}
			}
		
		gameView.update();
		gameModel.setupObservation();
		
		ChessGameLifecycle lifecycle = game_fac.createLifecycle(gameModel);
		
		return lifecycle;
	}

}
